package com.lms.service.impl;

import com.lms.bean.Role;
import com.lms.bean.Type;
import com.lms.dao.RoleDao;
import com.lms.service.RoleService;
import com.lms.vo.RoleVO;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;


    @Override
    public Long getTotalCount() {
        return roleDao.count();
    }

    @Override
    public List<RoleVO> findAll(int page, int pageSize) {
        Sort sort = Sort.by(Sort.Direction.ASC, "roleId");
        Pageable pageable = PageRequest.of(page, pageSize, sort);
        List<Role> list = roleDao.getList(pageable);
        List<RoleVO> roleVOList = new ArrayList<>();
        list.forEach(role -> roleVOList.add(RoleVO.convert(role)));
        return roleVOList;
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public void deleteById(Long roleId) {
        roleDao.deleteById(roleId);
    }

    @Override
    public Role findRoleById(Long roleId) {
        return roleDao.getOne(roleId);
    }

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }
}